<?php
/**
 * Introduction: 订单业务逻辑
 * @author: 杨陈鹏
 * @date: 2016/4/05 16:04
 * @email: yangchenpeng@cdlinglu.com
 */


namespace Common\Logic;

use Common\Model;

class OrderLogic
{

    static public $model, $model_extend;


    public function __construct()
    {

        self::$model = new Model\OrderModel();
        self::$model_extend = new Model\OrderExtendModel();

    }

    /**
     * @introduction: 订单列表
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param array $data 商品数据
     * @param int $uid 用户ID
     */
    static public function getData($where = [], $fields = '*', $order = 'create_time desc,status asc', $page = 1, $nums = 20)
    {

        if(!$where['status'])
            unset($where['status']);

        //$where['status'] = array('neq',7);

        $data = self::$model
            //->fetchSql(true)
            ->where($where)
            ->order($order)
            ->field($fields)
            ->page($page, $nums)
            ->select();

        $total = self::$model
            //->fetchSql(true)
            ->where($where)
            ->where("status != 7")
            ->count();

        if (!$data)
            return ['code' => 200, 'msg' => '暂无数据!'];

        //获取订单附属信息
        $goods_logic = D('Common/Goods', 'Logic');

        $member_logic = D('Common/Member','Logic');


        $harvestAddress_logic = D('Common/HarvestAddress','Logic');

        $volume_logic = D('Common/Volume','Logic');

        foreach ($data as $k => $v) {

            //获取用户信息
            $member_info = $member_logic::getOneData(['uid'=>$v['uid']],'username,mobile,nicname,uid');

            $data[$k]['member_info'] = $member_info;

            //获取收货地址信息
            $address_info = $harvestAddress_logic::getOneData(['id' => $v['addr_id']],'recipient,mobile,province_name,city_name,county_name,address');

            $data[$k]['address_info'] = $address_info['data'];

            //总价格等于订单价格加上总运费
            $data[$k]['order_price_total'] = intval($data[$k]['order_price_total']) + $data[$k]['freight'];
            //unset($data[$k]['freight']);
            //格式化下单时间
            $data[$k]['create_time'] = date('Y-m-d H:i:s', $data[$k]['create_time']);

            //优惠卷
            $volume = 0;

            if($v['volume_id'])
                $volume = $volume_logic::$model->where(['id' =>$v['volume_id']])->getField('price');

            $data[$k]['pay_total'] = intval($data[$k]['order_price_total']) - intval($volume);

            $data[$k]['order_extend'] = self::$model_extend
                ->where(['order_id' => $v['id']])
                ->field('price,nums,pay_total,freight,goods_id,goods_attr_val_ids')
                ->select();


            $attr_logic = D('Common/Attr', 'Logic');
            foreach ($data[$k]['order_extend'] as $key => $val) {

                $whr = "extend.is_default = 1";

                if ($val['goods_attr_val_ids'])
                    $whr = "extend.goods_attr_val_ids = '" . $val['goods_attr_val_ids'] . "'";

                $fileds = "goods.title,extend.thumb";

                $goods_info = $goods_logic::$model
                    ->alias('goods')
                    ->join('__GOODS_EXTEND__ extend ON goods.id = extend.goods_id', 'LEFT')
                    ->where("goods.id = " . $val['goods_id'] . " AND $whr")
                    ->field($fileds)
                    ->find();




                $data[$k]['order_extend'][$key]['title'] = $goods_info['title'];
                $data[$k]['order_extend'][$key]['thumb'] = $goods_info['thumb'];


                //商品单价为商品价格加上商品运费
                $data[$k]['order_extend'][$key]['_price'] = $data[$k]['order_extend'][$key]['price'];
                $data[$k]['order_extend'][$key]['price'] = intval($data[$k]['order_extend'][$key]['pay_total']) + intval($data[$k]['order_extend'][$key]['freight']);
                //unset($data[$k]['order_extend'][$key]['freight']);
                //unset($data[$k]['order_extend'][$key]['goods_attr_val_ids']);

                if ($val['goods_attr_val_ids']) {

                    $attr_val_ids = trim($val['goods_attr_val_ids'], ',');

                    $attr_val_info = $attr_logic::$model_attr_val
                        //->fetchSql(true)
                        ->where("id IN($attr_val_ids)")
                        ->field('name,id')
                        ->select();

//                    foreach ($attr_val_info as $v) {
//
//                        $attr .= $v['name'] . ' ';
//
//                    }

                    $data[$k]['order_extend'][$key]['attr'] = $attr_val_info;

                }

            }


        }

        //整理数据

        return ['code' => 200, 'data' => $data,'total' => $total];


    }

    /**
     * @introduction: 获取确认订单数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param array $data 商品数据
     * @param int $uid 用户ID
     */
    static public function comfirm($where)
    {


        $ids = $where['ids'];
        $uid = $where['uid'];

        if (!$ids)
            return self::_confirm($where['goods_id'], $where['goods_attr_val_ids'], $where['goods_num'], $where['uid'],$where['invitation']);

        $cart_logic = D('Common/Cart', 'Logic');

        $cart_info = $cart_logic::$model
            ->where("id IN($ids) AND uid = $uid")
            ->field('id,goods_id,goods_num,goods_attr_val_ids,invitation')
            ->select();

        if (!$cart_info)
            return ['code' => 300, 'msg' => '您的购物车还是空的哦!'];

        $attr_logic = D('Common/Attr', 'Logic');


        foreach ($cart_info as $k => $v) {

            $attr = '';

            if ($v['goods_attr_val_ids']) {

                $attr_val_ids = trim($v['goods_attr_val_ids'], ',');

                $attr_val_info = $attr_logic::$model_attr_val
                    //->fetchSql(true)
                    ->where("id IN($attr_val_ids)")
                    ->field('name')
                    ->select();

                foreach ($attr_val_info as $v) {

                    $attr .= $v['name'] . ' ';

                }

                $cart_info[$k]['attr'] = $attr;

            }

        }

        $goods_logic = D('Common/Goods', 'Logic');

        //$fields = 'id,title,thumb,description,goods_salenum';

        $goods_info = [];

        //检测商品是否已经下架
        $error = true;
        $error_info = '';

        foreach ($cart_info as $k => $v) {

            $whr = "extend.is_default = 1";

            if ($v['goods_attr_val_ids'])
                $whr = "extend.goods_attr_val_ids = '" . $v['goods_attr_val_ids'] . "'";

            $fileds = "'{$v['attr']}' as attr,{$v['id']} as cart_id, {$v['goods_num']} as goods_num,goods.title,extend.goods_id,extend.thumb,extend.goods_price,extend.goods_storage,extend.goods_attr_val_ids";

            $result = $goods_logic::$model
                ->alias('goods')
                //->fetchSql(true)
                ->join('__GOODS_EXTEND__ extend ON goods.id = extend.goods_id', 'LEFT')
                ->where("goods.id = " . $v['goods_id'] . " AND $whr")
                ->field($fileds)
                ->find();

            $result['invitation'] = $v['invitation'];

            $goods_info[] = $result;

            $res = self::checkGoodsStates($v['goods_id']);

            if(!$res){

                $error = false;

                $error_info[$v['goods_id']] = $result['title'];

            }


        }

        if(!$error){


            return ['code' => 300, 'msg' => '商品中存在已下架商品,请重新选购!', 'data' => $error_info];

        }


        //获取当前用户默认收货地址
        $address_logic = D('Common/HarvestAddress', 'Logic');

        $address = $address_logic::getOneData(['uid' => $uid]);

        if (!$goods_info)
            return ['code' => 300, 'msg' => '数据获取失败!'];


        return ['code' => 200, 'data' => ['goods_info' => $goods_info, 'address' => $address['data']]];


    }

    /**
     * @introduction: 直接购买确定订单
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param int $goods_id 商品ID
     * @param string $goods_id 商品ID
     * @param int $attr_val_ids 商品属性
     * @param int $goods_num 购买数量
     * @param int $goods_id 商品ID
     * @param int $uid 用户ID
     */
    static private function _confirm($goods_id, $goods_attr_val_ids, $goods_num, $uid,$invitation)
    {



        //检测当前商品是否已下架
        $res = self::checkGoodsStates($goods_id);


        if(!$res)
            return ['code' => 300,'msg' => '当前商品已下架,请重新选购!'];

        $attr = '';

        if ($goods_attr_val_ids) {

            $attr_logic = D('Common/Attr', 'Logic');

            $goods_attr_val_ids = trim($goods_attr_val_ids, ',');

            $attr_val_info = $attr_logic::$model_attr_val
                //->fetchSql(true)
                ->where("id IN($goods_attr_val_ids)")
                ->field('name')
                ->select();

            foreach ($attr_val_info as $v) {

                $attr .= $v['name'] . ' ';

            }

        }

        $goods_logic = D('Common/Goods', 'Logic');

        $whr = "extend.is_default = 1";

        if ($goods_attr_val_ids)
            $whr = "extend.goods_attr_val_ids = '{$goods_attr_val_ids}'";

        $fileds = "'{$attr}' as attr, $goods_num as goods_num,goods.title,extend.goods_id,extend.thumb,extend.goods_price,extend.goods_storage,extend.goods_attr_val_ids";

        $goods_info[] = $goods_logic::$model
            ->alias('goods')
            //->fetchSql(true)
            ->join('__GOODS_EXTEND__ extend ON goods.id = extend.goods_id', 'LEFT')
            ->where("goods.id = $goods_id AND $whr")
            ->field($fileds)
            ->find();
        $goods_info[0]['invitation'] = $invitation;


        //获取当前用户默认收货地址
        $address_logic = D('Common/HarvestAddress', 'Logic');

        $address = $address_logic::getOneData(['uid' => $uid]);

        return ['code' => 200, 'data' => ['goods_info' => $goods_info, 'address' => $address['data']]];

    }

    //检测商品是否下架
    static public function checkGoodsStates($goods_id)
    {


        $goods_logic = D('Common/Goods','Logic');

        $goods_info = $goods_logic::$model
                    ->where(['id' => $goods_id])
                    ->field('title,id,goods_state,publish_time')
                    ->find();


        if($goods_info['goods_state'] != 1 || $goods_info['publish_time'] > NOW_TIME)
            return false;

        return true;


    }

    /**
     * @introduction: 创建订单
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param array $data 创建订单所需要的数据
     * @param int $uid 创建订单所需要的数据
     */
    static public function create($data, $uid)
    {

        if (!$data['addr_id'])
            return ['code' => 300, 'msg' => '请选择收货地址'];

        if (!$data['express_id'])
            return ['code' => 300, 'msg' => '请选择物流方式'];

        if (!$data['goods_info'])
            return ['code' => 300, 'msg' => '购买商品信息不能为空'];

        $order_data['addr_id']          = $data['addr_id'];
        $order_data['express_id']       = $data['express_id'];
        $order_data['express_name']     = $data['express_name'];
        $order_data['create_time']      = NOW_TIME;
        $order_data['uid']              = $uid;
        $order_data['volume_id']        = $data['volume_id'] ? $data['volume_id'] : 0;

        $order_data['status']           = 1;



        $order_data['number']           = date('Ymdhis', NOW_TIME) . random(11);

        get_number:
        $order_data['number']           = date('Ymdhis', NOW_TIME) . random(11);

        //检测当前生成的订单号是否存在
        if (self::$model->where(['number' => $order_data['number']])->count())
            goto get_number;

        //获取商品信息

        //if(self::$model->add($order_data));

        $goods_logic = D('Common/Goods', 'Logic');

        $order_extend_data = [];


        //获取用户信息
        $member_logic = D('Common/Member','Logic');

        $micro_distribution = $member_logic::$model->where(['uid' => $uid])->getField('micro_distribution');

        //商品分销
        $distribution = D('Common/Distribution','Logic');
        //开启商品分销表事物
        $distribution::$model->startTrans();


        //检测商品是否已经下架
        $error = true;
        $error_info = [];


        foreach ($data['goods_info'] as $k => $v) {


            $whr = "extend.is_default = 1";

            if ($v['goods_attr_val_ids'])
                $whr = "extend.goods_attr_val_ids = '" . $v['goods_attr_val_ids'] . "'";

            $fileds = "extend.goods_id,goods.title,goods.is_distribution,extend.goods_price,extend.goods_storage,extend.goods_attr_val_ids";

            $goods_info = $goods_logic::$model
                ->alias('goods')
                //->fetchSql(true)
                ->join('__GOODS_EXTEND__ extend ON goods.id = extend.goods_id', 'LEFT')
                ->where("goods.id = " . $v['goods_id'] . " AND $whr")
                ->field($fileds)
                ->find();

            $res = self::checkGoodsStates($v['goods_id']);

            if(!$res){

                $error = false;
                $error_info[$goods_info['goods_id']] = $goods_info['title'];

            }

            //如果当前用户申请微分销通过,并且当前商品可以分销,将分销商品加入到当前用户的分销列表中
            if ( $goods_info['is_distribution'] == 2 && $micro_distribution == 9) {

                //检测当前商品是否已经存在当前用户的分销商品列表中
                if(!$distribution::$model->where(['uid' => $uid,'goods_id' => $goods_info['goods_id']])->count())
                {


                    $distribution_data                  = [];
                    $distribution_data['uid']           = $uid;
                    $distribution_data['goods_id']      = $goods_info['goods_id'];
                    $distribution_data['create_time']   = NOW_TIME;
                    $distribution::$model->add($distribution_data);


                }

            }

            //计算每个商品的运费
            $freight_data = [
                'goods_id' => intval($v['goods_id']),
                'county_id' => $data['county_id'],
                'city_id' => $data['city_id'],
                'province_id' => $data['province_id'],
                'goods_num' => intval($v['nums'])
            ];

            $freight = $goods_logic::freight($freight_data);

            //计算订单总运费
            $order_data['freight'] += intval($freight['data']['total']);


            $order_extend_data[$k]['goods_id']              = $goods_info['goods_id'];
            $order_extend_data[$k]['nums']                  = intval($v['nums']);
            $order_extend_data[$k]['message']               = $v['message'];
            $order_extend_data[$k]['price']                 = $goods_info['goods_price'];
            $order_extend_data[$k]['pay_total']             = intval($v['nums']) * intval($goods_info['goods_price']);
            $order_extend_data[$k]['freight']               = intval($freight['data']['total']);
            $order_extend_data[$k]['goods_attr_val_ids']    = $goods_info['goods_attr_val_ids'];
            $order_extend_data[$k]['invitation']            = $v['invitation'];

            //获取购物车信息  下单成功之后删除购物车对应商品
            $cart_info[$k]['goods_id']              = $goods_info['goods_id'];
            $cart_info[$k]['goods_attr_val_ids']    = $goods_info['goods_attr_val_ids'];

            //获取订单总价格
            $order_data['order_price_total']        += $order_extend_data[$k]['pay_total'];

            //检测商品库存
            if ($v['goods_num'] > $goods_info['goods_storage'])
                return ['code' => 300, 'msg' => "[" . $goods_info['title'] . '] 库存不足! '];

        }

        if(!$error)
            return ['code' => 300, 'msg' => '当前商品中有已下架商品,请重新选购!', 'data' => $error_info];

        if (!$order_id = self::$model->add($order_data))
            return ['code' => 300, 'msg' => '订单创建失败!'];


        //开启事物
        self::$model_extend->startTrans();

        foreach ($order_extend_data as $v) {

            $v['order_id'] = $order_id;

            $result[] = self::$model_extend->add($v);

        }

        //return $cart_info;

        //插入失败 开始回滚
        if (in_array(false, $result)) {

            self::$model_extend->rollback();

            //插入分销列表事物回滚
            $distribution::$model->rollback();

        }

        if (self::$model_extend->commit()) {

            $distribution::$model->commit();
            //下单成功  删除购物车

            $cart_logic = D('Common/Cart', 'Logic');

            foreach ($cart_info as $v) {

                $where = ['goods_id' => $v['goods_id'], 'goods_attr_val_ids' => $v['goods_attr_val_ids'], 'uid' => $uid];

                $cart_logic::$model->where($where)->delete();

            }

            //如果有使用优惠卷
            if($order_data['volume_id'])
            {

                $volume_logic = D('Common/Volume', 'Logic');

                $volume_logic::$model->where(['id' => $order_data['volume_id']])->setField('status',2);

            }

            return ['code' => 200, 'msg' => '订单创建成功!', 'data' => ['order_id' => $order_id]];

        }

        return ['code' => 300, 'msg' => '订单创建失败!'];


    }
    /**
     * @introduction: 检测商品是否下架
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function checkGoodsState($order_id)
    {

        if(!$order_id)
            return ['code' => 300, 'msg' => '缺少订单信息!'];



        $order_extend_data = self::$model_extend
            ->where(['order_id' => $order_id])
            ->field('goods_id,nums,goods_attr_val_ids')
            ->select();

        if(!$order_extend_data)
            return ['code' => 300, 'msg' => '订单不存在!'];


        $goods_logic = D('Common/Goods','Logic');

        $error = true;
        $error_info = '';

        foreach($order_extend_data as $v) {

            $goods_info = $goods_logic::$model
                                    ->where(['id' => $v['goods_id']])
                                    ->field('title,id,goods_state,publish_time')
                                    ->find();
            if($goods_info['goods_state'] != 1 || $goods_info['publish_time'] > NOW_TIME){

                $error_info[$v['id']] = $v['title'];

                $error = false;

            }

        }

        if($error){

            return ['code' => 200];

        }

        self::$model->where(['id' => $order_id])->save(['status' => 6,'close_time' => NOW_TIME]);

        //解冻资金
        self::thawCapital($order_id);

        return ['code' => 300, 'msg' => '订单中存在已下架商品,请重新下单!', 'data' => $error_info];

    }
    /**
     * @introduction: 检测库存
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function checkStorage($order_id){

        if(!$order_id)
            return ['code' => 300, 'msg' => '缺少订单信息!'];



        $order_extend_data = self::$model_extend
                                ->where(['order_id' => $order_id])
                                ->field('goods_id,nums,goods_attr_val_ids')
                                ->select();

        if(!$order_extend_data)
            return ['code' => 300, 'msg' => '订单不存在!'];

        $goods_logic = D('Common/Goods','Logic');

        $error = false;
        $error_info = '';

        foreach($order_extend_data as $v)
        {


            $whr = "extend.is_default = 1";

            if ($v['goods_attr_val_ids'])
                $whr = "extend.goods_attr_val_ids = '" . $v['goods_attr_val_ids'] . "'";

            $fileds = "extend.goods_id,goods.title,extend.goods_storage,extend.goods_attr_val_ids";

            $goods_info = $goods_logic::$model
                ->alias('goods')
                //->fetchSql(true)
                ->join('__GOODS_EXTEND__ extend ON goods.id = extend.goods_id', 'LEFT')
                ->where("goods.id = " . $v['goods_id'] . " AND $whr")
                ->field($fileds)
                ->find();

            if($v['nums'] > $goods_info['goods_storage'])
            {

                $error_info .= '|'.$goods_info['title'];
                $error = true;

            }

        }


        if($error)
            return ['code' => 300, 'msg' => '商品('.$error_info.") 库存不足!"];


        return ['code' => 200];



    }

    /**
     * @introduction: 计算订单总价
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function calcOrderTotalPrice($order_id)
    {

        if(!$order_id)
            return ['code' => 300 ,'msg' => '缺少订单信息!'];

        $order_info = self::$model
            ->where(['id' => $order_id])
            ->field('order_price_total,freight,number,volume_id,balance_pay_nums')
            ->find();

        if(!$order_info)
            return ['code' => 300, 'msg' => '订单不存在!'];

        $volume = 0;

        if($order_info['volume_id'])
        {

            //获取优惠卷信息
            $volume_logic = D('Common/Volume','Logic');

            $volume = $volume_logic::$model->where(['id' => $order_info['volume_id']])->getField('price');


        }



        $data['total'] = intval($order_info['order_price_total']) + intval($order_info['freight']) - $volume;

        $data['goods_price_total'] = intval($order_info['order_price_total']);

        $data['freight_total'] = intval($order_info['freight']);

        $data['number'] = $order_info['number'];

        $data['balance_pay_nums'] = $order_info['balance_pay_nums'];

        return ['code' => 200, 'data' => $data];

    }
    /**
     * @introduction: 修改订单状态
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param int $order_id 订单ID
     * @param int $status 状态值
     */
    static public function updateStatus($order_id,$status)
    {

        if(!$order_id)
            return ['code' => 300, 'msg' => '缺少订单信息'];

        if(!$status)
            return ['code' => 300, 'msg' => '参数错误!'];

        //return self::$model->fetchSql(true)->where(['id' => $order_id])->setField('status',$status);

        if(self::$model->where(['id' => $order_id])->setField('`status`',$status))
            return ['code' => 200, 'msg' => '操作成功!'];

        return ['code' => 300, 'msg' => '操作失败!'];


    }
    /**
     * @introduction: 修改订单
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param array or string $where 条件
     * @param array $data 需要修改的数据
     */
    static public function update($where,$data)
    {


        if(!$where)
            return ['code' => 300, 'msg' => '缺少订单信息!'];

        if(self::$model
                    ->where($where)
                    ->save($data))
            return ['code' => 200, 'msg' => '操作成功!'];

        return ['code' => 300, 'msg' => '操作失败!'];


    }
    /**
     * @introduction: 交易成功,减库存,加销量
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param array $data 创建订单所需要的数据
     * @param int $uid 创建订单所需要的数据
     */
    static public function success($where)
    {

        $order_id = self::$model->where($where)->getField('id');

        if(!$order_id)
            return false;

        //获取订单中的所有商品

        $order_extend = self::$model_extend
            ->where(['order_id' => $order_id])
            ->select();

        $goods_logic = D('Common/Goods','Logic');

        foreach($order_extend as $k => $v){

            $goods_logic::$model->where(['id' => $v['goods_id']])->setInc('goods_salenum',$v['nums']);

            $where = [];

            $where['goods_id'] = $v['goods_id'];
            if($v['goods_attr_val_ids'])
                $where['goods_attr_val_ids'] = $v['goods_attr_val_ids'];

            $goods_logic::$extend->where($where)->setDec('goods_storage',$v['nums']);

        }

        return true;



    }
    /**
     * @introduction: 获取订单详情
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param array $data 商品数据
     * @param int $uid 用户ID
     */
    static public function detail($order_id, $fields = '*')
    {
        if(!$order_id)
            return ['code' => 300, 'msg' => '缺少订单信息'];

        $order_info = self::$model
                        ->where(['id' => $order_id])
                        ->field($fields)
                        ->find();

        $order_info['extend'] = self::$model_extend
                        ->where(['order_id' => $order_info['id']])
                        ->select();

        //获取订单附属信息
        $goods_logic = D('Common/Goods', 'Logic');

        $member_logic = D('Common/Member','Logic');


        $harvestAddress_logic = D('Common/HarvestAddress','Logic');

        $attr_logic = D('Common/Attr', 'Logic');
        //获取用户信息
        $member_info = $member_logic::getOneData(['uid'=>$order_info['uid']],'username,mobile,nicname,uid');

        $order_info['member_info'] = $member_info;

        //获取收货地址信息
        $address_info = $harvestAddress_logic::getOneData(['id' => $order_info['addr_id']],'recipient,mobile,province_name,city_name,county_name,address');

        $order_info['address_info'] = $address_info['data'];




        foreach($order_info['extend'] as $k => $v)
        {

            $whr = "extend.is_default = 1";

            if ($v['goods_attr_val_ids'])
                $whr = "extend.goods_attr_val_ids = '" . $v['goods_attr_val_ids'] . "'";

            $fileds = "extend.goods_id,goods.title,extend.thumb";

            $goods_info = $goods_logic::$model
                ->alias('goods')
                //->fetchSql(true)
                ->join('__GOODS_EXTEND__ extend ON goods.id = extend.goods_id', 'LEFT')
                ->where("goods.id = " . $v['goods_id'] . " AND $whr")
                ->field($fileds)
                ->find();

            $order_info['extend'][$k]['goods_id'] = $goods_info['goods_id'];
            $order_info['extend'][$k]['title'] = $goods_info['title'];
            $order_info['extend'][$k]['thumb'] = $goods_info['thumb'];

            if ($v['goods_attr_val_ids']) {

                $attr_val_ids = trim($v['goods_attr_val_ids'], ',');

                $attr_val_info = $attr_logic::$model_attr_val
                    //->fetchSql(true)
                    ->where("id IN($attr_val_ids)")
                    ->field('name,id')
                    ->select();

//                    foreach ($attr_val_info as $v) {
//
//                        $attr .= $v['name'] . ' ';
//
//                    }

                $order_info['extend'][$k]['attr'] = $attr_val_info;

            }

        }

        return ['code' => 200, 'data' => $order_info];



    }
    /**
     * @introduction: 取消订单,解冻账户资金
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function thawCapital($order_id)
    {

        $balance_pay_nums = self::$model
            ->where(['id' => $order_id])
            ->field('balance_pay_nums,uid')
            ->find();

        $member_logic = D('Common/Member','Logic');

        if($member_logic::$model
                ->where(['uid' => $balance_pay_nums['uid']])
                ->setDec('frozen',$balance_pay_nums['balance_pay_nums']))
            return true;

        return false;


    }
    /**
     * @introduction: 订单流程
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param array $data 创建订单所需要的数据
     * @param int $uid 创建订单所需要的数据
     */
    public function process($order_id)
    {

        $order_info = self::$model->where(['id' =>$order_id])->find();

        $data = [];

        array_push($data,
            [
                'name' => '下单时间',
                'time' => date('Y-m-d H:i:s',$order_info['create_time'])
            ]
        );

        if($order_info['pay_time'])
            array_push($data,
                [
                    'name' => '买家支付',
                    'time' => date('Y-m-d H:i:s',$order_info['create_time'])
                ]
            );

        if($order_info['send_time'])
            array_push($data,
                [
                    'name' => '商家发货<br>
                               物流公司: '.$order_info['express_s_name'].'<br />
                               快递单号: '.$order_info['express_number'].'<br />
                                ',
                    'time' => date('Y-m-d H:i:s',$order_info['create_time'])
                ]
            );

        if($order_info['receipt_time'])
            array_push($data,
                [
                    'name' => '确认收货',
                    'time' => date('Y-m-d H:i:s',$order_info['create_time'])
                ]
            );

        if($order_info['apply_refund_time'])
            array_push($data,
                [
                    'name' => '申请退款',
                    'time' => date('Y-m-d H:i:s',$order_info['apply_refund_time'])
                ]
            );
        if($order_info['refund_time'])
            array_push($data,
                [
                    'name' => '退款时间',
                    'time' => date('Y-m-d H:i:s',$order_info['refund_time'])
                ]
            );
        if($order_info['complete_time'])
            array_push($data,
                [
                    'name' => '交易完成',
                    'time' => date('Y-m-d H:i:s',$order_info['complete_time'])
                ]
            );
        if($order_info['close_time'])
            array_push($data,
                [
                    'name' => '交易关闭',
                    'time' => date('Y-m-d H:i:s',$order_info['close_time'])
                ]
            );
        if($order_info['comment_time'])
            array_push($data,
                [
                    'name' => '评价时间',
                    'time' => date('Y-m-d H:i:s',$order_info['comment_time'])
                ]
            );



        return ['code' => 200, 'data' => $data];


    }
}